package stephen.competition14;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDao {
	
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	BaseDao basedao=new BaseDao();
	public User getUserByName(String name) throws SQLException{   
		BaseDao basedao=new BaseDao();
		con=basedao.getCon();
		String sql="select * from user where username=?";
		ps=con.prepareStatement(sql);
		ps.setString(1, name);
		rs=ps.executeQuery();
		String pwd="";
		
		while (rs.next()){    
			pwd=rs.getString("userpwd");   
			//id=r					
		}
		closeData();
		User u=new User(name,pwd);
		return u;
	}
	
	
	public boolean addcomment(String userName,String comment) throws SQLException{
		boolean result=false;
		String sql="insert into text (username,comment) values (?,?)";
		con=basedao.getCon(); 
		ps=con.prepareStatement(sql);
		ps.setString(1, userName);
		ps.setString(2, comment);
		if (ps.executeUpdate()>0){result=true;System.out.println("成功");}
		closeData();
		return result;
	}
	
	
	public void closeData() throws SQLException{
		if(rs!=null){
			rs.close();
		}
		if(ps!=null){
			ps.close();
		}
		if(con!=null){
			con.close();
		}
	}
}
